package edu.upc.clase.demo.controller;

import edu.upc.clase.demo.entity.Credential;
import edu.upc.clase.demo.entity.CriterioBusquedaAlquiler;
import edu.upc.clase.demo.entity.Alquiler;
import edu.upc.clase.demo.service.AlquilerService;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author cesar soriano
 */

@Controller("alquilerController")
public class AlquilerController {

    @Autowired
    private AlquilerService alquilerService;
    private static Logger log = LoggerFactory.getLogger(AlquilerController.class);

    @RequestMapping("/alquileres/index")
    public ModelAndView index() {
        ModelAndView mav = new ModelAndView("alquileres/index");
        List<Alquiler> alquileres = alquilerService.buscarTodos();
        CriterioBusquedaAlquiler criterioBusquedaAlquiler = new CriterioBusquedaAlquiler();
        mav.addObject("alquiler", alquileres);
        mav.addObject("criterioBusquedaAlquiler",criterioBusquedaAlquiler);
        return mav;
    }
    
    @RequestMapping(value = "/alquileres/new", method = RequestMethod.GET)
    public ModelAndView newAlquiler() {
        ModelAndView mav = new ModelAndView("alquileres/new");
        Alquiler alquiler = new Alquiler();
        mav.getModelMap().put("alquiler", alquiler);
        return mav;
    }

    @RequestMapping(value = "/alquileres/new", method = RequestMethod.POST)
    public String createAlquiler(@ModelAttribute("alquiler")Alquiler alquiler, SessionStatus status) {    
        alquilerService.insertar(alquiler);
        status.setComplete();

        return "redirect:/pages/alquileres/index";
    }
    
    @RequestMapping(value = "/alquileres/edit", method = RequestMethod.GET)
    public ModelAndView editAlquiler(@RequestParam("CoAlquiler")Integer CoAlquiler) {    
        ModelAndView mav = new ModelAndView("alquileres/edit");
        Alquiler alquiler = alquilerService.buscar(CoAlquiler);
        mav.getModelMap().put("alquiler", alquiler);
        return mav;        
    }
    
    @RequestMapping(value="/alquileres/edit", method=RequestMethod.POST)
    public String update(@ModelAttribute("alquiler") Alquiler alquiler, SessionStatus status) {
        alquilerService.actualizar(alquiler);
        status.setComplete();
        return "redirect:/pages/alquileres/index";
    }
    
    @RequestMapping("/alquileres/delete")
    public ModelAndView delete(@RequestParam("CoAlquiler")Integer CoAlquiler)
    {
        ModelAndView mav = new ModelAndView("redirect:/pages/alquileres/index");
        Alquiler alquiler = alquilerService.buscar(CoAlquiler);
        alquilerService.eliminar(alquiler);
        return mav;
    }
 
    
//    @RequestMapping(value = "/alquileres/buscar", method=RequestMethod.POST)
//    public ModelAndView buscar(@ModelAttribute("criterioBusqueda") CriterioBusqueda criterio, SessionStatus status) {
//        ModelAndView mav = new ModelAndView("alquileres/resultadoBusqueda");
//        log.info("resultado = " + alquilerService.buscarPorNombre(criterio.getNoUsuario()).size());
//        mav.getModel().put("resultado",alquilerService.buscarPorNombre(criterio.getNoUsuario()));
//        return mav;
//    }
    
}
